using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.IO;
public class DataUtil
{
    protected static SqlConnection conn;
    public static Boolean connect()
    {
        string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        try
        {
            conn = new SqlConnection(connectionString);
            conn.Open();
        }
        catch (Exception ex)
        {
            
            Console.WriteLine(ex.StackTrace.ToString());
            return false;
        }
        return true;
    }


    public static SqlConnection getConnection()
    {
        if (conn == null)
            connect();
        return conn;
    }


    public static DataTable executeStore(string spName, SqlParameter[] arrParam)
    {
        DataTable dt = null;
        if (connect())
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = conn;
                cmd.CommandText = spName;
                cmd.CommandTimeout = 1000;

                if (arrParam != null)
                {
                    foreach (SqlParameter param in arrParam)
                    {
                        cmd.Parameters.Add(param);
                    }
                }

                dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
            finally
            {
                conn.Close();
            }
        }
        return dt;
    }

    public static Boolean executeNonStore(string spName, SqlParameter[] arrParam)
    {
        if (connect())
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = conn;
                cmd.CommandText = spName;
                cmd.CommandTimeout = 1000;

                if (arrParam != null)
                {
                    foreach (SqlParameter param in arrParam)
                    {
                        cmd.Parameters.Add(param);
                    }
                }

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                //log.Error(ex.Message, ex);
                Console.WriteLine(ex.Message);
                return false;
            }
            finally
            {
                conn.Close();
            }
        }
        return true;
    }

   
}

